//
//  BaseViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/3.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "BaseViewController.h"
#import "AppDelegate.h"
#import "CNavBar.h"
#import "AlertWindow.h"
#import "LoadingView.h"
#import "HomeViewController.h"
#import "PersonCenterViewController.h"
#import "DiscussViewController.h"
#import <BaiduMapKit/BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapKit/BaiduMapAPI_Search/BMKGeocodeSearch.h>
@interface BaseViewController ()<BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate>
@property (nonatomic, strong) BMKLocationService *locService;
@property (nonatomic, strong) BMKGeoCodeSearch *codeSearch;
@property (nonatomic, strong) void (^finishLocation)(id);
@end
@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navBar setHidden:NO];
    self.view.backgroundColor=BackColor;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    ////推送信息
    AddNotification(NewMessage:, NSN_NewPushMessage);
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    RemoveNotification(NSN_NewPushMessage);
}

- (void)NewMessage:(NSNotification *)noti{
    [self newMessageAlert:noti.object];
}
- (void)newMessageAlert:(id)message{
//    [[self.mainTabBar tabBarItem:1] setUnReadCount:1];
}


- (BMKLocationService *)locService{
    if (_locService == nil) {
        _locService = [[BMKLocationService alloc] init];
        [_locService setDelegate:self];
        [_locService startUserLocationService];
    }
    return _locService;
}

- (BMKGeoCodeSearch *)codeSearch{
    if (_codeSearch == nil) {
        _codeSearch = [[BMKGeoCodeSearch alloc] init];
        [_codeSearch setDelegate:self];
    }
    return _codeSearch;
}


//实现相关delegate 处理位置信息更新
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_locService stopUserLocationService];
     echo(([NSString stringWithFormat:@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude]));
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    option.reverseGeoPoint = userLocation.location.coordinate;
    [self.codeSearch reverseGeoCode:option];
}

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (_finishLocation) {
        _finishLocation(@{@"city":result.addressDetail.city, @"province":result.addressDetail.province,@"latitude":[NSNumber numberWithDouble:result.location.latitude],@"longitude":[NSNumber numberWithDouble:result.location.longitude] });
    }
}

- (void) startLocation:(void (^)(id)) finish{
    [self.locService startUserLocationService];
    _finishLocation = finish;
}

- (MainNavBarViewController *)mainNavBarViewController{
    if (_mainNavBarViewController == nil) {
        _mainNavBarViewController = (MainNavBarViewController *)self.navigationController;
    }
    return _mainNavBarViewController;
}

- (void)setTitle:(NSString *)title{
    [super setTitle:title];
    if(title && ![title isEqualToString:@""])[self.navBar setTitle:title];
}

- (void)initView{
    [super initView];
    [self.navBar setBackTitle:@"返回"];
    if (self.navigationController.viewControllers.count == 1) {
        [self.navBar setLeftView:nil];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return CGColorEqualToColor(_navBar.bgColor.CGColor, [UIColor whiteColor].CGColor) ? UIStatusBarStyleDefault   : UIStatusBarStyleLightContent;
}
- (CNavBar *)navBar{
    if (_navBar == nil) {
        _navBar = [[CNavBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
        [self.view addSubview:_navBar];
        __block typeof(self) blockself = self;
        [_navBar setBackAction:^{
            [blockself setIsLoading:NO];
            [blockself backController];
            [[LoadingView shareLoading] stopAnimating];
        }];
        [_navBar setBgColor:[UIColor whiteColor]];
        
        [_navBar.lblTitle setTextColor:C(@"5a5a5a")];
        _navBar.backColor = MainColor;
    }
    if (![[self.view subviews] containsObject:_navBar]) {
        [self.view addSubview:_navBar];
    }
    return _navBar;
}
- (void)setHideToolBar:(BOOL)hideToolBar{
    [super setHideToolBar:hideToolBar];
    if (hideToolBar) {
        return;
    }
    static NSArray *controller;
    if (nil == controller) {
        MainNavBarViewController *(^cblock)(id) = ^(id controller){
            MainNavBarViewController *nav = (MainNavBarViewController *)self.navigationController;
            BaseViewController *lastc = [nav.viewControllers lastObject];
            if(![lastc isKindOfClass:[controller class]]){
                return [[MainNavBarViewController alloc] initWithRootViewController:controller];
            }
            return nav;
        };
        controller = @[@{@"title":@"首页", @"ico":@"Ico_TabBar_1", @"controller":cblock([HomeViewController new])},
                       @{@"title":@"讨论区", @"ico":@"Ico_TabBar_2", @"controller":cblock([DiscussViewController new])},
                       @{@"title":@"我的", @"ico":@"Ico_TabBar_3", @"controller":cblock([  PersonCenterViewController  new])},
                       ];

    }
    if (self.mainTabBar.tabBarItems.count > 0) {
        return;
    }
    for (NSDictionary *c in controller) {
        NSString *ico = c[@"ico"];
        NSString *ico_select = [NSString stringWithFormat:@"%@_Selected", ico];
        [self.mainTabBar addTabBarItem:[CTabBarItem initWithTitle:c[@"title"] image:[UIImage imageNamed:ico] selectedImage:[UIImage imageNamed:ico_select] controller:c[@"controller"]]];
    }
}

- (void)CTabBar:(CTabBar *)tabbar selectedTabBarItem:(CTabBarItem *)tabbaritem{
    NSArray *viewController =[(UINavigationController *)tabbaritem.controller viewControllers];
    BaseViewController *controller = [viewController firstObject];
    [controller.mainTabBar setSelectIndex:[tabbar.tabBarItems indexOfObject:tabbaritem]];
    [[[UIApplication sharedApplication] keyWindow] setRootViewController:tabbaritem.controller];
}



@end
